package demo.axitdn.model;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.view.MotionEvent;
import demo.axitdn.event.MultiEventListener;
import demo.axitdn.event.TouchListener;
import demo.axitdn.physic.Accumulator;
import demo.axitdn.physic.Force;
import demo.axitdn.physic.RK4V;
import demo.axitdn.physic.State;
import demo.axitdn.physic.Vector3f;

public abstract class Model implements TouchListener {
	private TouchListener touchListener;	
	protected State current;
	protected State previous;
	protected State state;
	protected RK4V rk4;
	protected List<RK4V> list=new ArrayList<RK4V>();
	protected float t=0;
	protected float dt = 0.01f;
	public Vector3f acc;
	public Model() {
		state=new State();
		current=state;
		previous=current;
		addTouchListener(this);
	}
	
	public void setA(Vector3f a) {
		acc=a;
	}
	
	public State getState() {
		return state;
	}
	
	public void setForce(Force f) {
		rk4=new RK4V(f);
	}
	
	
	
	public abstract void onDraw(Canvas c);
	public abstract void onUpdate(double t);
	
	
	
	public void integrate(double t,float dt) {	
		previous = current;			 
		rk4.integrate(current, t, dt);
	}
	
	public void interpolate(double alpha) {
		state=rk4.interpolate(previous, current, (float)alpha);	
		rk4.getForce().update(state);
		current=state;
		
	}
	
	public void addTouchListener(TouchListener l) {
		touchListener = MultiEventListener.add(touchListener, l);
    }

    public void removeTouchListener(TouchListener l) {
    	touchListener = MultiEventListener.remove(touchListener, l);
    }

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		return true;
	}


}
